package com.lingyu.jsjx.controller;

import java.util.List;
import java.io.IOException;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.lingyu.jsjx.domain.*;
import com.lingyu.jsjx.service.*;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 自我评价Controller
 *
 * @author ruoyi
 * @date 2025-08-08
 */
@RestController
@RequestMapping("/teacherSelfEvaluation")
public class TeacherSelfEvaluationController extends BaseController {
    @Autowired
    private ITeacherSelfEvaluationService teacherSelfEvaluationService;
    @Autowired
    private ITeacherPeerEvaluationService teacherPeerEvaluationService;
    @Autowired
    private ISupervisorEvaluationService supervisorEvaluationService;
    @Autowired
    private IStudentEvaluationService studentEvaluationService;
    @Autowired
    private IParentEvaluationService parentEvaluationService;

    /**
     * 查询自我评价列表
     */
    @RequiresPermissions("jsjx:teacherSelfEvaluation:list")
    @GetMapping("/list")
    public TableDataInfo list(TeacherSelfEvaluation teacherSelfEvaluation) {
        startPage();
        List<TeacherSelfEvaluation> list = teacherSelfEvaluationService.selectTeacherSelfEvaluationList(teacherSelfEvaluation);
        return getDataTable(list);
    }

    /**
     * 导出自我评价列表
     */
    @RequiresPermissions("jsjx:teacherSelfEvaluation:export")
    @Log(title = "自我评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TeacherSelfEvaluation teacherSelfEvaluation) {
        List<TeacherSelfEvaluation> list = teacherSelfEvaluationService.selectTeacherSelfEvaluationList(teacherSelfEvaluation);
        ExcelUtil<TeacherSelfEvaluation> util = new ExcelUtil<TeacherSelfEvaluation>(TeacherSelfEvaluation.class);
        util.exportExcel(response, list, "自我评价数据");
    }

    /**
     * 获取自我评价详细信息
     */
    @RequiresPermissions("jsjx:teacherSelfEvaluation:query")
    @GetMapping(value = "/{selfEvalId}")
    public AjaxResult getInfo(@PathVariable("selfEvalId") Long selfEvalId) {
        return success(teacherSelfEvaluationService.selectTeacherSelfEvaluationBySelfEvalId(selfEvalId));
    }

    /**
     * 新增自我评价
     */
    @RequiresPermissions("jsjx:teacherSelfEvaluation:add")
    @Log(title = "自我评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TeacherSelfEvaluation teacherSelfEvaluation) {
        return toAjax(teacherSelfEvaluationService.insertTeacherSelfEvaluation(teacherSelfEvaluation));
    }

    /**
     * 修改自我评价
     */
    @RequiresPermissions("jsjx:teacherSelfEvaluation:edit")
    @Log(title = "自我评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TeacherSelfEvaluation teacherSelfEvaluation) {
        return toAjax(teacherSelfEvaluationService.updateTeacherSelfEvaluation(teacherSelfEvaluation));
    }

    /**
     * 删除自我评价
     */
    @RequiresPermissions("jsjx:teacherSelfEvaluation:remove")
    @Log(title = "自我评价", businessType = BusinessType.DELETE)
    @DeleteMapping("/{selfEvalIds}")
    public AjaxResult remove(@PathVariable Long[] selfEvalIds) {
        return toAjax(teacherSelfEvaluationService.deleteTeacherSelfEvaluationBySelfEvalIds(selfEvalIds));
    }

    @GetMapping(value = "/getEthicsPerformStatistics")
    public AjaxResult getEthicsPerformStatistics() {
        Long total = 0L;
        int listSize = 0;
        String userId = String.valueOf(SecurityUtils.getUserId());
        TeacherSelfEvaluation teacherSelfEvaluation = new TeacherSelfEvaluation();
        teacherSelfEvaluation.setCreateBy(userId);
        TeacherPeerEvaluation teacherPeerEvaluation = new TeacherPeerEvaluation();
        teacherPeerEvaluation.setCreateBy(userId);
        SupervisorEvaluation supervisorEvaluation = new SupervisorEvaluation();
        supervisorEvaluation.setCreateBy(userId);
        StudentEvaluation studentEvaluation = new StudentEvaluation();
        studentEvaluation.setCreateBy(userId);
        ParentEvaluation parentEvaluation = new ParentEvaluation();
        parentEvaluation.setCreateBy(userId);
        List<TeacherSelfEvaluation> teacherSelfEvaluationList = teacherSelfEvaluationService.selectTeacherSelfEvaluationList(teacherSelfEvaluation);
        List<TeacherPeerEvaluation> teacherPeerEvaluationList = teacherPeerEvaluationService.selectTeacherPeerEvaluationList(teacherPeerEvaluation);
        List<SupervisorEvaluation> supervisorEvaluationList = supervisorEvaluationService.selectSupervisorEvaluationList(supervisorEvaluation);
        List<StudentEvaluation> studentEvaluationList = studentEvaluationService.selectStudentEvaluationList(studentEvaluation);
        List<ParentEvaluation> parentEvaluationList = parentEvaluationService.selectParentEvaluationList(parentEvaluation);
        if (!teacherSelfEvaluationList.isEmpty()) {
            total += teacherSelfEvaluationList.stream().filter(Objects::nonNull).mapToLong(TeacherSelfEvaluation::getEthicsPerformance).sum() / teacherSelfEvaluationList.size();
        }
        if (!teacherPeerEvaluationList.isEmpty()) {
            total += teacherPeerEvaluationList.stream().filter(Objects::nonNull).mapToLong(TeacherPeerEvaluation::getTeachingEthics).sum() / teacherPeerEvaluationList.size();
        }
        if (!supervisorEvaluationList.isEmpty()) {
            total += supervisorEvaluationList.stream().filter(Objects::nonNull).mapToLong(SupervisorEvaluation::getTeacherEthics).sum() / supervisorEvaluationList.size();
        }
        if (!studentEvaluationList.isEmpty()) {
            total += studentEvaluationList.stream().filter(Objects::nonNull).mapToLong(StudentEvaluation::getTeacherEthics).sum() / studentEvaluationList.size();
        }
        if (!parentEvaluationList.isEmpty()) {
            total += parentEvaluationList.stream().filter(Objects::nonNull).mapToLong(ParentEvaluation::getTeacherEthics).sum() / parentEvaluationList.size();

        }
        return success(total * 2);
    }
}
